package com.back.campus_back.service.impl;

import com.back.campus_back.domain.GoodsCategory;
import com.back.campus_back.mapper.GoodsCategoryMapper;
import com.back.campus_back.service.GoodsCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-17
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    //修改商品类别

    @Override
    public String updateByType1(String categoryName,Integer id) {

        //创建对象
        GoodsCategory category = new GoodsCategory();
        //构建更新条件
        UpdateWrapper<GoodsCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("category_id",id);
        //设置更新字段
        updateWrapper.set("category_name", categoryName);
        int update = goodsCategoryMapper.update(category, updateWrapper);
        //更新缓存
        redisTemplate.opsForValue().set(categoryName,id);
        if(update == 1){
            return "success";
        }else{
            return "error";
        }

    }

    //查询商品类型
    public List<GoodsCategory> findAllCategory(){
        return goodsCategoryMapper.selectList(null);

    }
}
